package ch05;

class ArrayEx07 {
	public static void main(String[] args) {
		char[] hex = { 'C', 'A', 'F', 'E' };

		String[] binary = { // hex 1 digit == binary 4 digit.
				//    0       1       2       3
					"0000", "0001", "0010", "0011",
                //	  4       5       6       7
					"0100", "0101",	"0110", "0111",
	            //	  8       9       A       B					
					"1000", "1001",	"1010", "1011",
                //	  C       D       E       F	
					"1100", "1101",	"1110", "1111" };

		String result = "";

		for (int i = 0; i < hex.length; i++) {
			if (hex[i] >= '0' && hex[i] <= '9')
				result += binary[hex[i] - '0'];
			else result += binary[hex[i] - 'A' + 10]; //hex 'A' == decimal 10
		}

		System.out.println("hex:" + new String(hex));
		System.out.println("binary:" + result);
	}
}
/*
hex:CAFE
binary:1100101011111110
*/